package xing.test.jingle;

import java.util.ArrayList;
import java.util.List;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.JingleSession;
import org.jivesoftware.smackx.jingle.JingleSessionRequest;
import org.jivesoftware.smackx.jingle.listeners.JingleSessionRequestListener;
import org.jivesoftware.smackx.jingle.media.JingleMediaManager;
import org.jivesoftware.smackx.jingle.mediaimpl.jmf.JmfMediaManager;
import org.jivesoftware.smackx.jingle.mediaimpl.jspeex.SpeexMediaManager;
import org.jivesoftware.smackx.jingle.mediaimpl.testxing.TestXingMediaManager;
import org.jivesoftware.smackx.jingle.nat.BasicTransportManager;

public class JingleReceiver {
	public static void main(String[] args) {
		try {

			// Connect to a XMPP Server
			XMPPConnection.DEBUG_ENABLED = true;
			XMPPConnection x1 = new XMPPConnection("10.10.9.104");
			x1.connect();
			x1.login("jtest", "jtest");

			BasicTransportManager icetm0 = new BasicTransportManager();
			List<JingleMediaManager> mediaManagers = new ArrayList<JingleMediaManager>();
			// mediaManagers.add(new JmfMediaManager(icetm0));
			mediaManagers.add(new TestXingMediaManager(icetm0));
			// mediaManagers.add(new ScreenShareMediaManager(icetm0));
			JingleManager jm = new JingleManager(x1, mediaManagers);
			// 注意。。一定要有下面这一句，在这里会注册对<jingle>标签的解析
			// -- providerManager.addIQProvider("jingle",
			// "urn:xmpp:tmp:jingle",new JingleProvider());
			// -- PacketReader 中会根据标签名称 jingle 和 标签属性 urn:xmpp:tmp:jingle 获取
			// 相应的provider对标签解析

			JingleManager.setJingleServiceEnabled();
			// jm.addCreationListener(icetm0);

			jm.addJingleSessionRequestListener(new JingleSessionRequestListener() {
				public void sessionRequested(JingleSessionRequest request) {
					try {
						JingleSession incoming = request.accept();
						incoming.startIncoming();
					} catch (XMPPException e) {
						e.printStackTrace();
					}

				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
